package com.taru.order.controller;

import com.taru.order.common.JsonResult;
import com.taru.order.entity.Group;
import com.taru.order.service.GruopServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GroupController {

    GruopServiceImpl gruopService=new GruopServiceImpl();

    /**
     * 获取专题称
     * @return
     */
    @RequestMapping(value = "/groups",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getGroups(){
        JsonResult result=null;
        try{
            List<Group> groupList =gruopService.getGroups();
            if(groupList !=null){
                result =new JsonResult(200, "获取成功",groupList);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 添加专题名称
     * @param group
     * @return
     */
    @RequestMapping(value = "/groups",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addGroup(@RequestBody Group group){
        JsonResult result=null;
        try{
            int code=gruopService.addGroup(group);
            if(code>0){
                result =new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改专题名称
     */
    @RequestMapping(value = "/groups",method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult modGroup(@RequestBody Group group){
        JsonResult result=null;
        try{
            int code=gruopService.modGroup(group);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 批量删除
     */
    @RequestMapping(value = "/groups",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult removeGroups(@RequestBody() List<Integer> list){
        JsonResult result=null;
        try{
            int code=gruopService.removeGroups(list);
            if(code>0){
                result =new JsonResult(200,"删除成功");
            }else {
                result=new JsonResult(404,"删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 依据专题（group_id）来查询文章列表
     */
    @RequestMapping(value = "/groups/{groupId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getGroupById(@PathVariable("groupId") String groupId){
        JsonResult result=null;
        try{
            Group  group=gruopService.getGroupById(groupId);
            if(group !=null){
                result =new JsonResult(200,"获取成功",group);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
}
